//
//  DCTextChatTableViewCell.m
//  xmppTest
//
//  Created by striby on 15/12/30.
//  Copyright © 2015年 com.dc. All rights reserved.
//

#import "DCTextChatTableViewCell.h"

@implementation DCTextChatTableViewCell




-(void)displayCell
{
  

    
    if(_messageDirection==DCChatDirectionLeft)
    {
        self.userIconImageView.frame = CGRectMake(baseCellMargin, baseCellMargin, userIconWidth, userIconWidth);

        self.chatTextView.backgroundImageView.image =[UIImage imageNamed:@"chat_from_bg_normal"];
        
    }
    else
    {
        self.userIconImageView.frame = CGRectMake(DCDeviceWidth-baseCellMargin-userIconWidth, baseCellMargin, userIconWidth, userIconWidth);
        
        self.chatTextView.backgroundImageView.image = [UIImage imageNamed:@"chat_to_bg_normal"];
    }
    
    
    if (self.isDisplayUserNickName) {
        self.nickNameLabel.hidden=NO;
        CGSize size = [self.nickNameLabel.text sizeWithAttributes:@{NSFontAttributeName:self.nickNameLabel.font}];
        
        if (_messageDirection==DCChatDirectionLeft) {
            
            [self.nickNameLabel setFrame:CGRectMake(self.userIconImageView.frame.origin.x+self.userIconImageView.frame.size.width+self.constant+baseCellMargin, self.userIconImageView.frame.origin.y, size.width, size.height)];
        }
        else
        {
             [self.nickNameLabel setFrame:CGRectMake(self.userIconImageView.frame.origin.x-self.constant-size.width, self.userIconImageView.frame.origin.y, size.width, size.height)];
        }
        
    }
    else
    {
        self.nickNameLabel.hidden=YES;
    }
    
    
    self.chatTextView.direction = (DCChatTextViewDirection)_messageDirection;
    
    
    CGFloat chatTextY = self.userIconImageView.frame.origin.y+(self.isDisplayUserNickName?[UIFont systemFontOfSize:nickNameFontSize].lineHeight+baseCellMargin:0);
    
    [self.chatTextView setText:_messageText atLeftTopPoint:CGPointMake(baseCellMargin*2+userIconWidth, chatTextY) andRightTopPoint:CGPointMake(DCDeviceWidth-(2*baseCellMargin)-userIconWidth, chatTextY)];
    
    [self.baseContentView setFrame:CGRectMake(0, self.isDisplaySendTime?(sendTimeBackgroundHeight+2*baseCellMargin):baseCellMargin,DCDeviceWidth, self.chatTextView.frame.size.height+baseCellMargin+self.chatTextView.frame.origin.y)];
    
    if (self.isDisplaySendTime) {
        
        CGSize size = [self.sendTimeLbl.text sizeWithAttributes:@{NSFontAttributeName:self.sendTimeLbl.font}];
        
        self.sendTimeBackgroundView.hidden=NO;
        self.sendTimeLbl.hidden=NO;
        
        [self.sendTimeBackgroundView setFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-(size.width+6))/2, baseCellMargin, size.width+6, sendTimeBackgroundHeight)];
        
        [self.sendTimeLbl setFrame:CGRectMake(2, 0, size.width+2, sendTimeBackgroundHeight)];
    }
    else
    {
        self.sendTimeBackgroundView.hidden=YES;
        self.sendTimeLbl.hidden=NO;
        
        
    }
    

    
}

-(void)loadBaseUI
{
    [super loadBaseUI];
    
    _chatTextView = [[DCChatTextView alloc]initWithFrame:CGRectZero];
    self.constant =6;
    _chatTextView.constant=self.constant;
    [self.baseContentView addSubview:_chatTextView];
    
}

-(CGFloat)cellHeight
{
    
    return self.baseContentView.frame.origin.y+self.baseContentView.frame.size.height+baseCellMargin+1;
    
}



-(CGFloat)heightFor:(XMPPMessageArchiving_Message_CoreDataObject *)messageObject
{
    XMPPMessage * message = messageObject.message;
    
    DCTextChatTableViewCell * cell = self;
    
    cell.messageText= message.body;
    
    XMPPJID * from ;
    if ([message elementForName:messageFrom]) {
       
        from = [XMPPJID jidWithString:[[message elementForName:messageFrom] stringValue]];
    }
    
    XMPPvCardTemp * card = [[DCXMPPTool shareInstance].xmppvCardTempModule vCardTempForJID:from!=nil?from:message.from shouldFetch:YES];
    
    if (messageObject.isOutgoing) {
        [cell setMessageDirection:DCChatDirectionRight];
        cell.isDisplayUserNickName=NO;
    }
    else
    {
        [cell setMessageDirection:DCChatDirectionLeft];
        cell.isDisplayUserNickName=YES;
        
        cell.nickNameLabel.text = card.nickname;
        
    }
    
    
    cell.isDisplaySendTime = NO;
    
    
    DDXMLElement * sendTime = [[message elementsForName:messageSendTime] firstObject];
    
    if (sendTime) {
        if (sendTime.stringValue) {
            cell.sendTimeLbl.text = sendTime.stringValue;
        }
    }
    
    if (card.photo) {
        cell.userIconImageView.image=[UIImage  imageWithData:card.photo];
    }
    else
    {
        cell.userIconImageView.image = [UIImage imageNamed:@"defaultUserIcon"] ;
    }
    
    
    [cell displayCell];
    
    return [cell cellHeight];
}

@end
